﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;

namespace GoodMan.Infrastructure.Entities.Cache
{
    /// <summary>
    /// 参数只包含一个，为productId
    /// </summary>
    public class ProductCache : EntitiesCache<Product>
    {
        /// <summary>
        /// key 为 username
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public override Product GetItem(params string[] keys)
        {
            return base.GetItem(keys);
        }
        protected override bool ValidateParameter(params string[] keys)
        {
            int productId = 0;
            return keys.Length == 1 && !String.IsNullOrEmpty(keys[0]) &&
                Int32.TryParse(keys[0], out productId);
        }
        protected override Product LoadDataItem(params string[] keys)
        {
            if (ValidateParameter(keys))
            {
                int productId = Int32.Parse(keys[0]);
                var product = new Product().Find(c => c.Id == productId);
                return product;
            }
            return null;
        }
        protected override string BuildKey(params string[] keys)
        {
            if (ValidateParameter(keys))
            {
                return String.Format("GetProductEntity_{0}", keys[0]);
            }
            return string.Empty;
        }
        protected override string BuildKey(Product data)
        {
            if (data != null && data.Id > 0)
            {
                return BuildKey(data.Id.ToString());
            }
            return string.Empty;
        }

    }
}
